//**** Frame FAS search and check module ***//

module FRM_FAS_MATCH(
   RESET,

   RCLK_155M,
   SFI4_RDATA,
   MPI_PAR_KEEP,

   FAS_LOF,
   FAS_OOF,
   FAS_MATCH_FCNT8,
   FAS_MATCH_FCNT270,
   FAS_MATCH_FCNT9,
   FAS_MATCH_DATA
   );

input              RESET;
input              RCLK_155M;
input[63:0]        SFI4_RDATA;
input[7:0]         MPI_PAR_KEEP;   // registers used for keep duplicated registers for timing 

output reg         FAS_LOF;
output reg         FAS_OOF;

output[2:0]        FAS_MATCH_FCNT8;
output[8:0]        FAS_MATCH_FCNT270;
output[3:0]        FAS_MATCH_FCNT9;
output[63:0]       FAS_MATCH_DATA;


reg[191:0]         MTCH_DATA_VECTOR;
reg[7:0]           MTCH_PAR_KEEP;
reg[135:1]         MTCH_MATCH_VECTOR0, MTCH_MATCH_VECTOR1, MTCH_MATCH_VECTOR2, MTCH_MATCH_VECTOR3;
reg[135:1]         MTCH_MATCH_VECTOR4, MTCH_MATCH_VECTOR5, MTCH_MATCH_VECTOR6, MTCH_MATCH_VECTOR7;

wire[2:0]          MTCH_MATCH_POINT7, MTCH_MATCH_POINT6, MTCH_MATCH_POINT5, MTCH_MATCH_POINT4;
wire[2:0]          MTCH_MATCH_POINT3, MTCH_MATCH_POINT2, MTCH_MATCH_POINT1, MTCH_MATCH_POINT0;
wire[7:0]          MTCH_MATCH_HIT;
reg[127:0]         MTCH_DATA_VECTOR_ME1, MTCH_DATA_VECTOR_ME2;

reg[127:0]         SELD_VECTOR_ME1, SELD_VECTOR_ME2, SELD_VECTOR_ME3, SELD_VECTOR_ME4;
reg[5:0]           SELD_MATCH_POINT;
reg                SELD_MATCH_HIT;
reg[5:0]           SELD_SEL_POINT;
reg[63:0]          SELD_SEL_DATA;

reg[2:0]           FCNT_CNT8;
reg[8:0]           FCNT_CNT270;
reg[3:0]           FCNT_CNT9;

wire               FCHK_RESTART;
reg                FCHK_FAS_ERR;
reg[4:0]           FCHK_LOF_CNT24;

always @(posedge RESET or posedge RCLK_155M) begin  // clock in SFI data and change it to 128bit data vector
   if ( RESET==1'b1 )
      MTCH_DATA_VECTOR[191:0]        <= 192'd0;
   else begin
      MTCH_DATA_VECTOR[191:128]      <= MTCH_DATA_VECTOR[127:64];
      MTCH_DATA_VECTOR[127:64]       <= MTCH_DATA_VECTOR[63:0];
      MTCH_DATA_VECTOR[63:0]         <= SFI4_RDATA[63:0];
   end
end

always @(posedge RESET or posedge RCLK_155M) begin : PAR_KEEP_CLOCK_IN // clock capture MPI PAR keep control bits
   if ( RESET==1'b1 )
      MTCH_PAR_KEEP[7:0]             <= 8'd0;
   else
      MTCH_PAR_KEEP[7:0]             <= MPI_PAR_KEEP[7:0];
end


//****** distribute data vector to 8*128bits match vector, in order to prevent equal registers removal, use enable bit from MPI
always @(posedge RESET or posedge RCLK_155M) begin : MACTH_VECTOR_DISTRIBUTE7
   if ( RESET==1'b1 )
      MTCH_MATCH_VECTOR7[135:1]      <= 135'd0;
   else begin
      if ( MTCH_PAR_KEEP[7]==1'b1 )
         MTCH_MATCH_VECTOR7[135:1]   <= MTCH_DATA_VECTOR[191:57];
      else
         MTCH_MATCH_VECTOR7[135:1]   <= 135'd0;
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : MACTH_VECTOR_DISTRIBUTE6
   if ( RESET==1'b1 )
      MTCH_MATCH_VECTOR6[135:1]      <= 135'd0;
   else begin
      if ( MTCH_PAR_KEEP[6]==1'b1 )
         MTCH_MATCH_VECTOR6[135:1]   <= MTCH_DATA_VECTOR[183:49];
      else
         MTCH_MATCH_VECTOR6[135:1]   <= 135'd0;
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : MACTH_VECTOR_DISTRIBUTE5
   if ( RESET==1'b1 )
      MTCH_MATCH_VECTOR5[135:1]      <= 135'd0;
   else begin
      if ( MTCH_PAR_KEEP[5]==1'b1 )
         MTCH_MATCH_VECTOR5[135:1]   <= MTCH_DATA_VECTOR[175:41];
      else
         MTCH_MATCH_VECTOR5[135:1]   <= 135'd0;
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : MACTH_VECTOR_DISTRIBUTE4
   if ( RESET==1'b1 )
      MTCH_MATCH_VECTOR4[135:1]      <= 135'd0;
   else begin
      if ( MTCH_PAR_KEEP[4]==1'b1 )
         MTCH_MATCH_VECTOR4[135:1]   <= MTCH_DATA_VECTOR[167:33];
      else
         MTCH_MATCH_VECTOR4[135:1]   <= 135'd0;
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : MACTH_VECTOR_DISTRIBUTE3
   if ( RESET==1'b1 )
      MTCH_MATCH_VECTOR3[135:1]      <= 135'd0;
   else begin
      if ( MTCH_PAR_KEEP[3]==1'b1 )
         MTCH_MATCH_VECTOR3[135:1]   <= MTCH_DATA_VECTOR[159:25];
      else
         MTCH_MATCH_VECTOR3[135:1]   <= 135'd0;
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : MACTH_VECTOR_DISTRIBUTE2
   if ( RESET==1'b1 )
      MTCH_MATCH_VECTOR2[135:1]      <= 135'd0;
   else begin
      if ( MTCH_PAR_KEEP[2]==1'b1 )
         MTCH_MATCH_VECTOR2[135:1]   <= MTCH_DATA_VECTOR[151:17];
      else
         MTCH_MATCH_VECTOR2[135:1]   <= 135'd0;
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : MACTH_VECTOR_DISTRIBUTE1
   if ( RESET==1'b1 )
      MTCH_MATCH_VECTOR1[135:1]      <= 135'd0;
   else begin
      if ( MTCH_PAR_KEEP[1]==1'b1 )
         MTCH_MATCH_VECTOR1[135:1]   <= MTCH_DATA_VECTOR[143:9];
      else
         MTCH_MATCH_VECTOR1[135:1]   <= 135'd0;
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : MACTH_VECTOR_DISTRIBUTE0
   if ( RESET==1'b1 )
      MTCH_MATCH_VECTOR0[135:1]      <= 135'd0;
   else begin
      if ( MTCH_PAR_KEEP[0]==1'b1 )
         MTCH_MATCH_VECTOR0[135:1]   <= MTCH_DATA_VECTOR[135:1];
      else
         MTCH_MATCH_VECTOR0[135:1]   <= 135'd0;
   end
end

FRM_FAS_COMPARE        INST7_FRM_FAS_COMPARE(
   .RESET              ( RESET ),
   .RCLK_155M          ( RCLK_155M ),

   .MATCH_VECTOR       ( MTCH_MATCH_VECTOR7[135:1] ),
   .MATCH_HIT          ( MTCH_MATCH_HIT[7] ),
   .MATCH_POINT        ( MTCH_MATCH_POINT7[2:0] )
   );
FRM_FAS_COMPARE        INST6_FRM_FAS_COMPARE(
   .RESET              ( RESET ),
   .RCLK_155M          ( RCLK_155M ),

   .MATCH_VECTOR       ( MTCH_MATCH_VECTOR6[135:1] ),
   .MATCH_HIT          ( MTCH_MATCH_HIT[6] ),
   .MATCH_POINT        ( MTCH_MATCH_POINT6[2:0] )
   );
FRM_FAS_COMPARE        INST5_FRM_FAS_COMPARE(
   .RESET              ( RESET ),
   .RCLK_155M          ( RCLK_155M ),

   .MATCH_VECTOR       ( MTCH_MATCH_VECTOR5[135:1] ),
   .MATCH_HIT          ( MTCH_MATCH_HIT[5] ),
   .MATCH_POINT        ( MTCH_MATCH_POINT5[2:0] )
   );
FRM_FAS_COMPARE        INST4_FRM_FAS_COMPARE(
   .RESET              ( RESET ),
   .RCLK_155M          ( RCLK_155M ),

   .MATCH_VECTOR       ( MTCH_MATCH_VECTOR4[135:1] ),
   .MATCH_HIT          ( MTCH_MATCH_HIT[4] ),
   .MATCH_POINT        ( MTCH_MATCH_POINT4[2:0] )
   );
FRM_FAS_COMPARE        INST3_FRM_FAS_COMPARE(
   .RESET              ( RESET ),
   .RCLK_155M          ( RCLK_155M ),

   .MATCH_VECTOR       ( MTCH_MATCH_VECTOR3[135:1] ),
   .MATCH_HIT          ( MTCH_MATCH_HIT[3] ),
   .MATCH_POINT        ( MTCH_MATCH_POINT3[2:0] )
   );
FRM_FAS_COMPARE        INST2_FRM_FAS_COMPARE(
   .RESET              ( RESET ),
   .RCLK_155M          ( RCLK_155M ),

   .MATCH_VECTOR       ( MTCH_MATCH_VECTOR2[135:1] ),
   .MATCH_HIT          ( MTCH_MATCH_HIT[2] ),
   .MATCH_POINT        ( MTCH_MATCH_POINT2[2:0] )
   );
FRM_FAS_COMPARE        INST1_FRM_FAS_COMPARE(
   .RESET              ( RESET ),
   .RCLK_155M          ( RCLK_155M ),

   .MATCH_VECTOR       ( MTCH_MATCH_VECTOR1[135:1] ),
   .MATCH_HIT          ( MTCH_MATCH_HIT[1] ),
   .MATCH_POINT        ( MTCH_MATCH_POINT1[2:0] )
   );
FRM_FAS_COMPARE        INST0_FRM_FAS_COMPARE(
   .RESET              ( RESET ),
   .RCLK_155M          ( RCLK_155M ),

   .MATCH_VECTOR       ( MTCH_MATCH_VECTOR0[135:1] ),
   .MATCH_HIT          ( MTCH_MATCH_HIT[0] ),
   .MATCH_POINT        ( MTCH_MATCH_POINT0[2:0] )
   );

always @(posedge RESET or posedge RCLK_155M) begin
   if ( RESET==1'b1 )  begin
      MTCH_DATA_VECTOR_ME1[127:0]           <= 128'd0;
      MTCH_DATA_VECTOR_ME2[127:0]           <= 128'd0;
   end
   else begin
      MTCH_DATA_VECTOR_ME1[127:0]           <= MTCH_DATA_VECTOR[127:0];
      MTCH_DATA_VECTOR_ME2[127:0]           <= MTCH_DATA_VECTOR_ME1[127:0];
   end
end


//****** data select output ******//
always @(posedge RESET or posedge RCLK_155M) begin : SELD_HIT_CHECK
   if ( RESET==1'b1 )
      SELD_MATCH_HIT                 <= 1'b0;
   else begin
      case ( MTCH_MATCH_HIT[7:0] )
      8'b1000_0000:   SELD_MATCH_HIT        <= 1'b1;
      8'b0100_0000:   SELD_MATCH_HIT        <= 1'b1;
      8'b0010_0000:   SELD_MATCH_HIT        <= 1'b1;
      8'b0001_0000:   SELD_MATCH_HIT        <= 1'b1;
      8'b0000_1000:   SELD_MATCH_HIT        <= 1'b1;
      8'b0000_0100:   SELD_MATCH_HIT        <= 1'b1;
      8'b0000_0010:   SELD_MATCH_HIT        <= 1'b1;
      8'b0000_0001:   SELD_MATCH_HIT        <= 1'b1;
      default     :   SELD_MATCH_HIT        <= 1'b0;
      endcase
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : SELD_POINT_ENCODE
   if ( RESET==1'b1 )
      SELD_MATCH_POINT[5:0]                 <= 6'd0;
   else begin
      case ( MTCH_MATCH_HIT[7:0] )
      8'b1000_0000:   SELD_MATCH_POINT[5:0] <= {3'b111, MTCH_MATCH_POINT7[2:0]};
      8'b0100_0000:   SELD_MATCH_POINT[5:0] <= {3'b110, MTCH_MATCH_POINT6[2:0]};
      8'b0010_0000:   SELD_MATCH_POINT[5:0] <= {3'b101, MTCH_MATCH_POINT5[2:0]};
      8'b0001_0000:   SELD_MATCH_POINT[5:0] <= {3'b100, MTCH_MATCH_POINT4[2:0]};
      8'b0000_1000:   SELD_MATCH_POINT[5:0] <= {3'b011, MTCH_MATCH_POINT3[2:0]};
      8'b0000_0100:   SELD_MATCH_POINT[5:0] <= {3'b010, MTCH_MATCH_POINT2[2:0]};
      8'b0000_0010:   SELD_MATCH_POINT[5:0] <= {3'b001, MTCH_MATCH_POINT1[2:0]};
      8'b0000_0001:   SELD_MATCH_POINT[5:0] <= {3'b000, MTCH_MATCH_POINT0[2:0]};
      default     :   SELD_MATCH_POINT[5:0] <= 6'd0;
      endcase
   end
end

always @(posedge RESET or posedge RCLK_155M) begin
   if ( RESET==1'b1 )
      SELD_SEL_POINT[5:0]            <= 6'd0;
   else begin
      if ( FCHK_RESTART==1'b1 && SELD_MATCH_HIT==1'b1 )
         SELD_SEL_POINT[5:0]         <= SELD_MATCH_POINT[5:0];
   end
end

always @(posedge RESET or posedge RCLK_155M) begin
   if ( RESET==1'b1 ) begin
      SELD_VECTOR_ME1[127:0]         <= 128'd0;
      SELD_VECTOR_ME2[127:0]         <= 128'd0;
   end
   else begin
      SELD_VECTOR_ME1[127:0]         <= MTCH_DATA_VECTOR_ME2[127:0];
      SELD_VECTOR_ME2[127:0]         <= SELD_VECTOR_ME1[127:0];
   end
end

always @(posedge RESET or posedge RCLK_155M) begin
   if ( RESET==1'b1 )
      SELD_SEL_DATA[63:0]           <= 64'd0;
   else begin
      case ( SELD_SEL_POINT[5:0] )
      6'b111111:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[127:64];
      6'b111110:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[126:63];
      6'b111101:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[125:62];
      6'b111100:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[124:61];
      6'b111011:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[123:60];
      6'b111010:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[122:59];
      6'b111001:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[121:58];
      6'b111000:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[120:57];
      6'b110111:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[119:56];
      6'b110110:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[118:55];
      6'b110101:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[117:54];
      6'b110100:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[116:53];
      6'b110011:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[115:52];
      6'b110010:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[114:51];
      6'b110001:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[113:50];
      6'b110000:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[112:49];

      6'b101111:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[111:48];
      6'b101110:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[110:47];
      6'b101101:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[109:46];
      6'b101100:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[108:45];
      6'b101011:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[107:44];
      6'b101010:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[106:43];
      6'b101001:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[105:42];
      6'b101000:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[104:41];
      6'b100111:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[103:40];
      6'b100110:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[102:39];
      6'b100101:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[101:38];
      6'b100100:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[100:37];
      6'b100011:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[99:36];
      6'b100010:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[98:35];
      6'b100001:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[97:34];
      6'b100000:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[96:33];

      6'b011111:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[95:32];
      6'b011110:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[94:31];
      6'b011101:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[93:30];
      6'b011100:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[92:29];
      6'b011011:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[91:28];
      6'b011010:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[90:27];
      6'b011001:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[89:26];
      6'b011000:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[88:25];
      6'b010111:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[87:24];
      6'b010110:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[86:23];
      6'b010101:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[85:22];
      6'b010100:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[84:21];
      6'b010011:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[83:20];
      6'b010010:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[82:19];
      6'b010001:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[81:18];
      6'b010000:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[80:17];

      6'b001111:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[79:16];
      6'b001110:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[78:15];
      6'b001101:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[77:14];
      6'b001100:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[76:13];
      6'b001011:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[75:12];
      6'b001010:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[74:11];
      6'b001001:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[73:10];
      6'b001000:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[72:9];
      6'b000111:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[71:8];
      6'b000110:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[70:7];
      6'b000101:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[69:6];
      6'b000100:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[68:5];
      6'b000011:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[67:4];
      6'b000010:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[66:3];
      6'b000001:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[65:2];
      6'b000000:  SELD_SEL_DATA[63:0]           <=SELD_VECTOR_ME2[64:1];
      endcase
   end
end



//****** frame counter generate ******//
always @(posedge RESET or posedge RCLK_155M) begin : COUNTER_64BYTES
   if ( RESET==1'b1 )
      FCNT_CNT8[2:0]           <= 3'd0;
   else begin
      if ( SELD_MATCH_HIT==1'b1 && FCHK_RESTART==1'b1)
          FCNT_CNT8[2:0]       <= 3'd0;
      else
          FCNT_CNT8[2:0]       <= FCNT_CNT8[2:0] +3'd1;
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : COUNTER_ROW_270
   if ( RESET==1'b1 )
      FCNT_CNT270[8:0]         <= 9'd0;
   else begin
      if ( SELD_MATCH_HIT==1'b1 && FCHK_RESTART==1'b1) begin  // SELD_MATCH_HIT indicate the last "F6" of FAS
         FCNT_CNT270[8:0]      <= 9'd3;
      end
      else if ( FCNT_CNT8[2:0]==3'b111 ) begin
         if ( FCNT_CNT270[8:0] ==9'd269 )
            FCNT_CNT270[8:0]   <= 9'd0;
         else
            FCNT_CNT270[8:0]   <= FCNT_CNT270[8:0] +9'd1;
      end
   end
end
always @(posedge RESET or posedge RCLK_155M) begin : COUNTER_COL_9
   if ( RESET==1'b1 )
      FCNT_CNT9[3:0]           <= 4'd0;
   else begin
      if ( SELD_MATCH_HIT==1'b1 && FCHK_RESTART==1'b1) begin
         FCNT_CNT9[3:0]        <= 4'd0;
      end
      else if ( FCNT_CNT8[2:0]==3'b111 && FCNT_CNT270[8:0] ==9'd269 ) begin
         if ( FCNT_CNT9[3:0]==4'd8 )
            FCNT_CNT9[3:0]     <= 4'd0;
         else
            FCNT_CNT9[3:0]     <= FCNT_CNT9[3:0] +4'd1;
      end
   end
end
  assign   FAS_MATCH_FCNT8[2:0]   =FCNT_CNT8[2:0];
  assign   FAS_MATCH_FCNT270[8:0] =FCNT_CNT270[8:0];
  assign   FAS_MATCH_FCNT9[3:0]   =FCNT_CNT9[3:0];
  assign   FAS_MATCH_DATA[63:0]   =SELD_SEL_DATA[63:0];



//****** generate FAS check reslt, count the frames without correct FAS ******//
always @( FCNT_CNT9 or FCNT_CNT270 or SELD_SEL_DATA) begin
      if ( FCNT_CNT9[3:0]==4'd0 && ( FCNT_CNT270[8:0] ==9'd0 || FCNT_CNT270[8:0] ==9'd1 || FCNT_CNT270[8:0] ==9'd2 ) ) begin
         if ( SELD_SEL_DATA[63:0]!=64'hF6F6F6F6_F6F6F6F6 )
            FCHK_FAS_ERR       <= 1'b1;
      end
      else if ( FCNT_CNT9[3:0]==4'd0 && ( FCNT_CNT270[8:0] ==9'd3 || FCNT_CNT270[8:0] ==9'd4 || FCNT_CNT270[8:0] ==9'd5 ) ) begin
         if ( SELD_SEL_DATA[63:0]!=64'h28282828_28282828 )
            FCHK_FAS_ERR       <= 1'b1;
      end
      else
            FCHK_FAS_ERR       <= 1'b0;
end
always @(posedge RESET or posedge RCLK_155M) begin
   if ( RESET==1'b1 )
      FCHK_LOF_CNT24[4:0]             <= 5'd23;
   else begin
      if ( FCHK_LOF_CNT24[4:0]>=5'd3 && SELD_MATCH_HIT==1'b1 )  // OOF or LOF condition
         FCHK_LOF_CNT24[4:0]          <= 5'd0;
      else if ( FCNT_CNT8[2:0]==3'd0 && FCNT_CNT270[8:0] ==9'd8 && FCNT_CNT8[2:0]==3'd7 ) begin
         if ( FCHK_FAS_ERR==1'b1 && FCHK_LOF_CNT24[4:0]!=5'd23 )
            FCHK_LOF_CNT24[4:0]       <= FCHK_LOF_CNT24[4:0] +5'd1;
         else
            FCHK_LOF_CNT24[4:0]       <= 5'd0;
      end
   end
end

always @(posedge RESET or posedge RCLK_155M) begin
   if ( RESET==1'b1 )
      FAS_LOF                  <= 1'b1;
   else begin
      if ( FCHK_LOF_CNT24[4:0]==5'd23 ) begin
         if ( SELD_MATCH_HIT==1'b1 )
            FAS_LOF            <= 1'b0;
         else
            FAS_LOF            <= 1'b1;
      end
      else
         FAS_LOF               <= 1'b0;
   end
end
always @(posedge RESET or posedge RCLK_155M) begin
   if ( RESET==1'b1 )
      FAS_OOF                  <= 1'b1;
   else begin
      if ( FCHK_LOF_CNT24[4:0]>=5'd23 ) begin
         if ( SELD_MATCH_HIT==1'b1 )
            FAS_OOF            <= 1'b0;
         else
            FAS_OOF            <= 1'b1;
      end
      else
          FAS_OOF              <= 1'b0;
   end
end
  assign  FCHK_RESTART  =FAS_OOF;


endmodule
